home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 January: Mac OS SDK / Dev.CD Jan 97 SDK1.toast / Development Kits (Disc 1) / AppleScript / Development Tools / Sample Code / Sample OSA Component / In MW C with Tester / MyOSAComponent.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-11-08  |  9.1 KB  |  357 lines  |  [TEXT/MMCC]

  1. /*
  2.  File MyOSAComponent.c
  3.  
  4.  
  5.  
  6.  Copyright © 1992-1993 Apple Computer, Inc. All rights reserved.}
  7.  
  8.  This is a sample program to implement the OSA interface. 
  9.  In this sample program, the source is the same as the internal compiled form. 
  10.  So compile and decompile just consists of copying and rename, and this is very 
  11.  unlike other OSA implementations. However, there is a lot of scripting language 
  12.  which just interprets the source and this sample program can be useful.
  13.  
  14.  Ported over to C 9/20/95
  15.  R.Silva
  16.  
  17.  
  18.  
  19. */
  20.  
  21. #ifdef THINK_C
  22. #define        applec
  23. #endif
  24.  
  25. //#include     "myHeaders.h"
  26.  
  27. #include    <Memory.h>
  28. #include    <Errors.h>
  29. #include    <Components.h>
  30.  
  31.  
  32. #include <Dialogs.h>
  33. #include <AppleEvents.h> 
  34. #include <OSA.h> 
  35. #include <OSAComp.h>
  36. #include <TextUtils.h>
  37.  
  38. #define  DEBUG_IT  //comment this out when building scripting component
  39.                     //keep it defined when debugging the code   
  40.  
  41. #include    "MyOSAComponent.h"
  42. #include    "MyOSAComponentRoutines.h"
  43.  
  44.  
  45.  
  46.  
  47. /* ------------------------------------------------------------------------- */
  48.  
  49. //    Component entry point.
  50.  
  51.  
  52.  
  53.  
  54.  
  55. #ifdef DEBUG_IT
  56.  
  57. pascal ComponentResult OSADispatcher(ComponentParameters *params, char **storage)
  58.  
  59. #else
  60.  
  61. pascal ComponentResult main(ComponentParameters *params, char **storage)
  62.  
  63. #endif DEBUG_IT
  64. {
  65.     long                ret;
  66.     ComponentResult   err;
  67.  
  68.     if ( kDEBUGME ){
  69.         DebugStr("\pIn main()");
  70.         
  71.     }
  72.     
  73.     if ( params->what < 0 ) { 
  74.         switch ( params->what ) {
  75.             case kComponentOpenSelect:
  76.                 if ( kDEBUGME )
  77.                     DebugStr("\pOpenConnection");
  78.                 err = CallComponentFunction(params, (ComponentFunctionUPP) MyOpen);
  79.                 break;
  80.             
  81.             case kComponentCloseSelect:
  82.                 if ( kDEBUGME )
  83.                     DebugStr("\pCloseConnection");
  84.                 err = CallComponentFunctionWithStorage(storage, params,
  85.                         (ComponentFunctionUPP) MyClose) ;
  86.                 break;    
  87.                             
  88.             case kComponentCanDoSelect:
  89.                 if ( kDEBUGME )
  90.                     DebugStr("\pCanDo");
  91.                 err = CallComponentFunction(params, (ComponentFunctionUPP) MyCanDo);
  92.                 break;
  93.     
  94.             case kComponentVersionSelect:
  95.                   if ( kDEBUGME )
  96.                 DebugStr("\pComponentVersionSelect");
  97.                 err = CallComponentFunction(params, (ComponentFunctionUPP) MyVersion);
  98.                 break;
  99.                 
  100.             case kComponentRegisterSelect: 
  101.              if ( kDEBUGME )
  102.                 DebugStr("\pComponentRegisterSelect");
  103.                 err = CallComponentFunction(params, (ComponentFunctionUPP) MyRegister);
  104.                 break;
  105.                 
  106.             case kComponentTargetSelect:
  107.                 if ( kDEBUGME )
  108.                 DebugStr("\pComponentTargetSelect");   
  109.                 err = CallComponentFunctionWithStorage(storage, params,
  110.                         (ComponentFunctionUPP) MyTarget);
  111.                 break;
  112.                 
  113.             default:
  114.                 err = paramErr;
  115.         }
  116.     } else {
  117.         switch ( params->what ) {
  118.             
  119.             case kOSASelectLoad: 
  120.                 if ( kDEBUGME )
  121.                     DebugStr("\pSelectLoad");
  122.                 err = CallComponentFunctionWithStorage(storage, params, (ComponentFunctionUPP) DoOSALoad);
  123.                 break;
  124.                 
  125.             case kOSASelectStore: 
  126.                 if ( kDEBUGME )
  127.                     DebugStr("\pSelectStore");
  128.                 err = CallComponentFunctionWithStorage(storage, params, (ComponentFunctionUPP)DoOSAStore);
  129.                 break;
  130.                 
  131.             case kOSASelectExecute:        
  132.                 if ( kDEBUGME )
  133.                     DebugStr("\pSelectExecute");
  134.                 err = CallComponentFunctionWithStorage(storage, params, (ComponentFunctionUPP)DoOSAExecute);
  135.                 break;
  136.                 
  137.             case kOSASelectDisplay:
  138.                 if ( kDEBUGME )
  139.                     DebugStr("\pSelectDisplay");
  140.                 err = CallComponentFunctionWithStorage(storage, params, (ComponentFunctionUPP)DoOSADisplay);
  141.                 break;
  142.                 
  143.             case kOSASelectScriptError: 
  144.                 if ( kDEBUGME )
  145.                     DebugStr("\pScriptError");
  146.                 err = CallComponentFunctionWithStorage(storage, params, (ComponentFunctionUPP)DoOSAScriptError);
  147.                 break;
  148.                 
  149.             case kOSASelectDispose:
  150.                 if ( kDEBUGME )
  151.                     DebugStr("\pSelectDispose");
  152.                 err = CallComponentFunctionWithStorage(storage, params, (ComponentFunctionUPP)DoOSADispose);
  153.                 break;
  154.                 
  155.             case kOSASelectCompile: 
  156.                 if ( kDEBUGME )
  157.                     DebugStr("\pSelectCompile");
  158.                 err = CallComponentFunctionWithStorage(storage, params, (ComponentFunctionUPP)DoOSACompile);
  159.                 break;
  160.                 
  161.             case kOSASelectGetSource:
  162.                 if ( kDEBUGME )
  163.                     DebugStr("\pSelectGetSource");     
  164.                 err = CallComponentFunctionWithStorage(storage, params, (ComponentFunctionUPP)DoOSAGetSource);
  165.                 break;
  166.                 
  167.             case kOSASelectScriptingComponentName:
  168.                  if ( kDEBUGME )
  169.                     DebugStr("\pSelectScriptName");
  170.                 err = CallComponentFunctionWithStorage(storage, params, (ComponentFunctionUPP)DoOSAScriptingComponentName);
  171.                 break;
  172.                 
  173.             case kOSASelectCoerceFromDesc: 
  174.                  if ( kDEBUGME )
  175.                     DebugStr("\pSelectCoerceFromDesc");
  176.                 err = CallComponentFunctionWithStorage(storage, params, (ComponentFunctionUPP)DoOSACoerceFromDesc);
  177.                 break;
  178.         
  179.             case kOSASelectCoerceToDesc: 
  180.                 if ( kDEBUGME )
  181.                     DebugStr("\pSelectCoerceToDesc");
  182.                 err = CallComponentFunctionWithStorage(storage, params, (ComponentFunctionUPP)DoOSACoerceToDesc);
  183.                 break;
  184.                 
  185.             case kOSASelectLoadExecute: 
  186.                 if ( kDEBUGME )
  187.                     DebugStr("\pSelectLoadExecute");
  188.                 err = CallComponentFunctionWithStorage(storage, params, (ComponentFunctionUPP)DoOSALoadExecute);
  189.                 break;
  190.                 
  191.             case kOSASelectCompileExecute: 
  192.                 if ( kDEBUGME )
  193.                     DebugStr("\pSelectCompileExecute");
  194.                 err = CallComponentFunctionWithStorage(storage, params, (ComponentFunctionUPP)DoOSACompileExecute);
  195.                 break;
  196.                 
  197.             case kOSASelectDoScript: 
  198.                 if ( kDEBUGME )
  199.                     DebugStr("\pSelectDoScript");  
  200.                 err = CallComponentFunctionWithStorage(storage, params, (ComponentFunctionUPP)DoOSADoScript);
  201.                 break;
  202.                 
  203.             case kOSASelectMakeContext: 
  204.                 if ( kDEBUGME )
  205.                     DebugStr("\pSelectMakeContext");    
  206.                 err = CallComponentFunction(params, (ComponentFunctionUPP)DoOSAMakeContext);
  207.                 break;
  208.                 
  209.             case kOSASelectSetScriptInfo:
  210.                 if ( kDEBUGME )
  211.                     DebugStr("\pSelectSetScriptInfo");
  212.                 err = CallComponentFunctionWithStorage(storage, params, (ComponentFunctionUPP)DoOSASetScriptInfo);
  213.                 break;
  214.                 
  215.             case kOSASelectGetScriptInfo:
  216.                 if ( kDEBUGME )
  217.                     DebugStr("\pSelectGetScriptInfo");
  218.                 err = CallComponentFunctionWithStorage(storage, params, (ComponentFunctionUPP)DoOSAGetScriptInfo);
  219.                 break;
  220.                 
  221.             case kOSASelectSetActiveProc:
  222.             case kOSASelectGetActiveProc:
  223.                 err = noErr;
  224.                 break;
  225.                                 
  226.             default:
  227.                 return( permErr );
  228.         }
  229.     }
  230.     return err;
  231. }
  232.  
  233. /* ------------------------------------------------------------------------- */
  234.  
  235. //    Required component calls.
  236.  
  237.  
  238. pascal ComponentResult MyOpen( ComponentInstance self)
  239. {
  240.     GlobalsHandle   globals;
  241.     ComponentResult   err;
  242.  
  243.      err = noErr;
  244.     globals = (GlobalsHandle)   NewHandleClear(  sizeof(GlobalsRecord)   ) ;
  245.     if( globals ==  nil ) { 
  246.         err = memFullErr ; //  some memory error
  247.     }
  248.     else {
  249.         HLock((Handle)globals);
  250.         //blank error message
  251.         err = AECreateDesc(typeChar, " ", 1, &((*globals)->errorDesc));
  252.         HUnlock((Handle)globals);
  253.         if( err == noErr) { 
  254.             HLock((Handle)globals);
  255.             (*globals)->gSelf =  self;
  256.             HUnlock((Handle)globals);
  257.             SetComponentInstanceStorage(self, (Handle) globals);
  258.         }
  259.         else {
  260.             DisposeHandle((Handle) globals);
  261.             err = memFullErr; 
  262.         }
  263.     }         
  264.     return err;
  265. }
  266.  
  267. pascal ComponentResult MyClose(Handle myGlobals, ComponentInstance self)
  268. {
  269.     int i;
  270.     GlobalsHandle globals;
  271.     
  272.     globals = (GlobalsHandle) myGlobals;
  273.     
  274.     HLock((Handle)globals);
  275.     AEDisposeDesc(&((*globals)->errorDesc));
  276.     //dispose all the descriptors in the slots if the handles are not nil
  277.     //we have to make sure all the unused slots have nil handles
  278.     for( i = 0 ; i< MaxIDSlot; i++ ) {
  279.         if( (((*globals)->scriptIDSlot)[i]).dataHandle != nil){
  280.             AEDisposeDesc(&((*globals)->scriptIDSlot)[i]);
  281.             (((*globals)->scriptIDSlot)[i]).dataHandle = nil;
  282.         }
  283.     }
  284.     HUnlock( (Handle) globals);
  285.     DisposeHandle((Handle) globals);
  286.     return  noErr;
  287. }
  288.  
  289. pascal ComponentResult MyCanDo(short selector)
  290. {    
  291.     if ( kDEBUGME )
  292.         DebugStr("\pIn MyCanDo()");
  293.     
  294.     switch ( selector ) {
  295.     
  296.         //    Required component calls.
  297.         case kComponentOpenSelect:
  298.         case kComponentCloseSelect:
  299.         case kComponentCanDoSelect:
  300.         case kComponentVersionSelect: 
  301.         case kComponentRegisterSelect: 
  302.         case kComponentTargetSelect:
  303.         
  304.  
  305.         //    MyComponent specific calls.
  306.         case kOSASelectLoad:
  307.         case kOSASelectStore:
  308.         case kOSASelectExecute:
  309.         case kOSASelectDisplay:
  310.         case kOSASelectScriptError:
  311.         case kOSASelectDispose:
  312.         case kOSASelectCompile:
  313.         case kOSASelectGetSource:
  314.         case kOSASelectScriptingComponentName:
  315.         case kOSASelectCoerceFromDesc:
  316.         case kOSASelectCoerceToDesc:
  317.         case kOSASelectLoadExecute:
  318.         case kOSASelectCompileExecute:
  319.         case kOSASelectDoScript:
  320.         case kOSASelectMakeContext:
  321.         //DebugStr("\pIn the can do");     
  322.             return ( true );
  323.  
  324.         //    Not handled.
  325.         default:
  326.             return ( false );
  327.     }
  328. }
  329.  
  330. pascal ComponentResult MyVersion()
  331. {
  332.     if ( kDEBUGME )
  333.         DebugStr("\pIn MyVersion()");
  334.  
  335.     return ( (kMyComponentSpec<<16) | (kMyComponentVersion) );
  336. }
  337.  
  338. pascal ComponentResult MyRegister()
  339. {
  340.     if ( kDEBUGME )
  341.         DebugStr("\pIn MyRegister()");
  342.  
  343.     return ( false );
  344. }
  345.  
  346. pascal ComponentResult MyTarget(Handle storage, ComponentInstance self)
  347. {
  348.     GlobalsHandle         globals = (GlobalsHandle) storage;
  349.     
  350.     if ( kDEBUGME )
  351.         DebugStr("\pIn MyTarget()");
  352.  
  353.     //    From now on, self will be the component instance that targeted us.
  354.     (**globals).gSelf = self;
  355.     
  356.     return ( noErr );
  357. }